import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';

import '../../network/Api.dart';
import '../../network/http_config.dart';
import '../../utils/loading.dart';
import '../../utils/provider.dart';

class Invite2Ui extends StatelessWidget {
  const Invite2Ui({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const _Invite2Widget();
  }
}

class _Invite2Widget extends StatefulWidget {
  const _Invite2Widget({Key? key}) : super(key: key);

  @override
  State<_Invite2Widget> createState() => _Invite2WidgetState();
}

class _Invite2WidgetState extends State<_Invite2Widget> {

  String _inviteCode = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getQr();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Stack(
      children: [
        Container(
          width: size.width,
          height: size.height,
          color: const Color(0xFFfba4ce),
        ),
        SingleChildScrollView(
          child: Stack(
            alignment: AlignmentDirectional.topCenter,
            children: [
              Image.asset(
                "images/ic_invite_bg.png",
                width: size.width,
                height: size.width / 1242 * 2210,
                fit: BoxFit.fill,
              ),
              Positioned(
                  top: size.width / 1242 * 1030,
                  child: QrImageView(
                    padding: const EdgeInsets.all(0),
                    backgroundColor: Colors.white,
                    data: _getQrInfo(),
                    version: QrVersions.auto,
                    size: 367 / 1242 * size.width,
                    eyeStyle: QrEyeStyle(
                      eyeShape: QrEyeShape.square,
                      color: ThemeColor.mainColor,
                    ),
                    dataModuleStyle: const QrDataModuleStyle(
                      dataModuleShape: QrDataModuleShape.circle,
                      color: Colors.black,
                    )
                  ),
              )
            ],
          ),
        ),

      ],
    );
  }

  String _getQrInfo() {
    return _inviteCode.isEmpty ? "" : HttpConfig.baseUrlProxy + "?inviteCode=$_inviteCode";
  }

  _getQr() {
    loading(() async {
      final res = await Api.getInstance().getMemberInfo(Provider.memberId);
      if (res?['data']?['shareCode']?.toString()?.isNotEmpty == true) {
        setState(() {
          _inviteCode = res['data']['shareCode'];
        });
      } else {
        toast("获取邀请码失败！");
      }
    });
  }
}
